import socket
# AF_INET为选择IPV4传输
# SOCK_STREAM 为选择流式传输
# 创建套接字对象
server_socket= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
# 绑定地址和端口
server_socket.bind(("127.0.0.1", 8808))
# 设置监听
server_socket.listen(1024)

# 阻塞等待处理连接
print("waiting for connection...")
conn, addr = server_socket.accept()
print("connection from", addr)

# f=open("from_client.png", "wb")
# 收发消息

data = conn.recv(4096)
if data:
    print("get_data from:",data)
    # f.write(data)
    # HTTP/1.1 404 Not Found 响应行
    data_to_send = """HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n
    
    NI HAO NV XIN ZHE"""
    n=conn.send(data_to_send.encode())
    print("发送内容:",data_to_send.encode())
    conn.close()





